﻿using Newtonsoft.Json;
using WebSocketSharp.Server;

namespace OfficeMiddlewareCore
{
    /// <summary>
    /// 返回模型
    /// </summary>
    public class WebSocketResponse
    {
        public static WebSocketServer Server { get; set; }
        public WebSocketResponse()
        {

        }
        public WebSocketResponse(CoreResponseStatus status, WsDto dto, object returnData)
        {
            Code = status;
            Message = status.ToString();
            RequestData = dto;
            Result = returnData;
            DocCode = dto.DocCode;
        }
        public WebSocketResponse(CoreResponseStatus status, string docCode)
        {
            Code = status;
            Message = status.ToString();
            DocCode = docCode;
        }
        public CoreResponseStatus Code { get; set; }
        public string DocCode { get; set; }
        /// <summary>
        /// 执行完成后，返回的消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 返回结果,主要用在判断文件是否打开，，文件是否存在。如果有打开或存在返回 1，否则为0
        /// </summary>
        public object Result { get; set; }

        /// <summary>
        /// 请求数据,web端请求过来的数据模型。原样返回
        /// </summary>
        public WsDto RequestData { get; set; }

        public static void Create(CoreResponseStatus status, string docCode, string message = null)
        {
            var result = new WebSocketResponse(status, docCode);
            result.Message = message;
            var json = JsonConvert.SerializeObject(result);
            Server.WebSocketServices.Broadcast(json);
        }

        public static void Create(CoreResponseStatus status, WsDto dto = null)
                        => Create(status, dto, null);

        public static void Create(CoreResponseStatus status, WsDto dto, object returnData = null)
        {
            var result = new WebSocketResponse(status, dto, returnData);
            var json = JsonConvert.SerializeObject(result);
            Server.WebSocketServices.Broadcast(json);
        }

    }
}
